package cn.afactory.tygz.ui.base.views.search;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.utils.SystemUtils;

/**
 * Created by wangyouguo on 15/6/3.
 */
public class SearchTypePopWindow extends PopupWindow implements
        AdapterView.OnItemClickListener {

    private ListView mListView;
    private ListDataAdapter mAdapter;
    private List<DataEntity> mData = new ArrayList<DataEntity>();
    private OnChangeListener mListener;
    private LayoutInflater mInflater;
    private Context mContext;
    private static int MAX_WIN_HEIGHT;
    private static final int MARGIN = 10;

    public static class DataEntity {
        // add for jkcard
        public long categoryId;
        public boolean isSelected;
        public String id;
        public String displayText;
    }

    private OnDismissListener onDismissListener = new OnDismissListener() {
        @Override
        public void onDismiss() {
            if (mListener != null) {
                mListener.onDismiss();
            }
        }
    };

    public SearchTypePopWindow(Context context) {
        super(context);
        mContext = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new BitmapDrawable());
        MAX_WIN_HEIGHT = SystemUtils.getSreenHeight(context)
                - (int) mContext.getResources().getDimension(
                R.dimen.order_status_keep_bottom_height);
        setView();
    }

    public void setView() {
        View view = mInflater.inflate(R.layout.view_search_popup_win_layout, null);
        initView(view);
        setContentView(view);
    }

    private void initView(View view) {
        mListView = (ListView) view.findViewById(R.id.list_view);
        mAdapter = new ListDataAdapter();
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        setOnDismissListener(onDismissListener);
    }

    public <T extends DataEntity> void setData(List<T> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        mAdapter.notifyDataSetChanged();
    }

    public void setDefaultSelection(SearchType type) {
        for (DataEntity item : mData) {
            if (item.categoryId == type.getId()) {
                item.isSelected = true;
            } else {
                item.isSelected = false;
            }
        }
    }

    public void showWindowInCenter(View view) {
        showAsDropDown(view, mContext.getResources().
                getDimensionPixelSize(R.dimen.margin_10dp),
                -mContext.getResources().getDimensionPixelSize(
                R.dimen.margin_10dp));
        int width = view.getResources().getDimensionPixelSize(R.dimen.margin_100dp);
        int itemHeight = (int) mContext.getResources().getDimension(
                R.dimen.margin_45dp);
        int height = mAdapter.getCount() * itemHeight + (int)mContext.getResources()
                .getDimension(R.dimen.margin_20dp);
        update(width, height > MAX_WIN_HEIGHT ? MAX_WIN_HEIGHT : height);
        if (height > MAX_WIN_HEIGHT) {
            mListView.setScrollContainer(true);
        }
    }

    public void setOnChangeListener(OnChangeListener l) {
        mListener = l;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (mListener != null) {
            mListener.onItemClick(adapterView, view, i, l, mAdapter.getItem(i));
            setSelectState(i);
        }
        dismiss();
    }

    private void setSelectState(int pos) {
        for (int i = 0; i < mAdapter.getCount(); i++) {
            mAdapter.getItem(i).isSelected = (i == pos);
        }
    }

    private class ListDataAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mData == null ? 0 : mData.size();
        }

        @Override
        public DataEntity getItem(int i) {
            return mData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = mInflater.inflate(R.layout.search_type_item,
                    null);
            TextView tv = ((TextView) view.findViewById(R.id.item));
            tv.setText(getItem(i).displayText);
            boolean sel = getItem(i).isSelected;
            tv.setSelected(sel);

            return view;
        }
    }

    public static interface OnChangeListener {
        public void onItemClick(AdapterView<?> adapterView, View view, int i,
                                long l, DataEntity item);

        public void onDismiss();
    }
}
